package wyp.library.ui.dialog;

import wyp.library.R;
import wyp.library.tools.WypLog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

/**
 * 下载时的弹窗，可以显示下载的进度。
 * 
 * 显示弹窗：WypDownloadDialog _dialog = WypDownloadDialog.showDialog(activity);
 * 更新弹窗进度：_dialog.setPercent(0.1f); 关闭弹窗：WypDownloadDialog.closeDialog();
 * 
 * @author wuyiping
 * 
 */
public class WypDownloadDialog extends Dialog {

	public static final String TAG = WypDownloadDialog.class.getSimpleName();

	private static WypDownloadDialog dialog;
	private WypDownloadDialogView mDialogView;

	/**
	 * 显示弹窗
	 * 
	 * @param __context
	 * @return
	 */
	public static WypDownloadDialog showDialog(Context __context) {
		if (dialog == null) {
			dialog = new WypDownloadDialog(__context);
		} else {
			try {
				WypDownloadDialog.closeDialog();
			} catch (Exception e) {
			}
		}

		dialog.show();
		return dialog;
	}

	private WypDownloadDialog(Context context) {
		super(context, R.style.custom_dialog);
		setCanceledOnTouchOutside(false);
		setCancelable(false);
	}

	/**
	 * 关闭弹窗
	 */
	public static void closeDialog() {
		dialog.dismiss();
		dialog = null;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.wyp_download_dialog);
		mDialogView = (WypDownloadDialogView) super.findViewById(R.id.dialog_view);
	}

	/**
	 * 更新弹窗进度
	 * 
	 * @param __percent
	 *            必须在0-1之间
	 */
	public void setPercent(float __percent) {
//		WypLog.e(TAG, "" + __percent);
		if (__percent >= 0 && __percent <= 1) {
			mDialogView.setPercent(__percent);
		}
	}

}
